/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/17 22:46
 */
package org.mechanic.fund.web.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.mechanic.core.web.MediaTypes;
import org.mechanic.fund.domain.system.manager.User;
import org.mechanic.fund.service.product.impl.UserService;
import org.mechanic.fund.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户接口
 *
 * @author mechanic
 * @version 0.0.1
 */
@Api("用户信息接口")
@RestController
@RequestMapping(value = "user")
public class SystemUserContorller extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(SystemUserContorller.class);

    private final UserService userService;

    @Autowired
    public SystemUserContorller(UserService userService) {this.userService = userService;}

    @ApiOperation(value = "返回匹配用户编号的用户信息")
    @GetMapping(value = "{id}", produces = MediaTypes.JSON_UTF_8)
    public Object details(
      @ApiParam(value = "用户编号", required = true) @PathVariable(required = true) String id
    ) {
        return result(this.userService.findById(id));
    }

    @ApiOperation(value = "返回用户信息分页列表")
    @GetMapping(value = "list", produces = MediaTypes.JSON_UTF_8)
    public Object list(
      @ApiParam(value = "页码(从0开始)", required = false) @RequestParam(required = false, defaultValue = "0") Integer page
    ) {
        Page<User> userPage = this.userService.getList(super.pageRequest(page));
        return result(userPage);
    }

    @ApiOperation(value = "删除匹配用户编号的用户")
    @GetMapping(value = "delete", produces = MediaTypes.JSON_UTF_8)
    public Object delete(
      @ApiParam(value = "用户编号", required = true) @PathVariable(required = true) String id
    ) {
        this.userService.delete(id);
        return result();
    }

    @ApiOperation(value = "批量保存用户信息")
    @PostMapping(value = "saves", consumes = MediaTypes.JSON_UTF_8)
    public Object saves(
      @ApiParam(value = "用户编号", required = true) @RequestBody(required = true) List<User> users
    ) {
        this.userService.save(users, super.currUser().getId());
        return result();
    }

    @ApiOperation(value = "保存用户信息")
    @PostMapping(value = "save", consumes = MediaTypes.JSON_UTF_8)
    public Object save(
      @ApiParam(value = "用户编号", required = true) @RequestBody(required = true) User user
    ) {
        String password = user.getPassword();
        Assert.notNull(password, "密码不能为空。");
        RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
        user.setSalt(randomNumberGenerator.nextBytes().toHex());
        String encryptPassword = new SimpleHash(
          "SHA-1",
          password,
          ByteSource.Util.bytes(user.getSalt()),
          3).toHex();
        return result(this.userService.save(user, super.currUser().getId()));
    }
}
